package com.tjy.wfx.service.impl;

import com.tjy.wfx.dao.SysRoleFunMapper;
import com.tjy.wfx.dao.SysRoleMapper;
import com.tjy.wfx.model.Result;
import com.tjy.wfx.model.SysRole;
import com.tjy.wfx.model.SysRoleFun;
import com.tjy.wfx.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleFunMapper sysRoleFunMapper;

    @Override
    public List<SysRole> findRoles() {
        return sysRoleMapper.selectAll();
    }

    @Override
    public SysRole findById(String roleCode) {
        return sysRoleMapper.selectByPrimaryKey(roleCode);
    }

    @Override
    public Result savePerms(String roleId, String[] moduleArry) {
        //通过角色id到(角色菜单关系表中把所有菜单删除)
        Example example = new Example(SysRoleFun.class);
        Example.Criteria and = example.and();
        and.andEqualTo("roleId", roleId);
        sysRoleFunMapper.deleteByExample(example);
        //创建(角色菜单关系表 把所有 角色id 菜单id 添加进去)
        SysRoleFun sysRoleFun = new SysRoleFun();
        for (String s : moduleArry) {
            sysRoleFun.setRoleId(roleId);
            sysRoleFun.setModuleId(s);
            sysRoleFunMapper.insert(sysRoleFun);
        }

        return new Result("操作成功", true);

    }


}
